package com.brush.web.controller;

import com.brush.app.domain.req.Course.add.AppCourseReq;
import com.brush.app.domain.req.Course.edit.AppCourseEditReq;
import com.brush.app.domain.resp.course.AppCourseResp;
import com.brush.app.domain.resp.course.AppCourseTreeResp;
import com.brush.app.service.AppCourseService;
import com.brush.app.service.AppSubjectService;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "后台-课程管理")
@RestController
@RequestMapping(value = "/admin/brush/course")
public class AdminCourseController extends BaseController {
    @Autowired
    private AppCourseService appCourseService;


//appCourseService    //获取左边的全部course类型
//    @PreAuthorize("@ss.hasPermi('app:course:list')")
//    @ApiOperation(value = "获取全部学科和课程")
//    @RequestMapping("/getAllCourse")
//    public R<List<AppCourseTreeResp>> tree(){
//        List<AppCourseTreeResp> resp =  this.appCourseService.allCourseTree();
//        return R.ok(resp);
//    }


    //查询表格树
    @ApiOperation(value = "查询表格树")
    @GetMapping(value = "/list/{subjectId}")
    @PreAuthorize("@ss.hasPermi('app:course:list')")
    public R<List<AppCourseTreeResp>> searchTree(@PathVariable(value = "subjectId") Long subjectId){
        List<AppCourseTreeResp> list =this.appCourseService.searchTableTree(subjectId);
        return R.ok(list);
    }
    //根据id查询
    @ApiOperation(value = "根据id查询课程")
    @PreAuthorize("@ss.hasPermi('app:course:query')")
    @GetMapping("/{id}")
    public R<AppCourseResp> findById(@PathVariable(value = "id") Long id){
        AppCourseResp resp = this.appCourseService.findById(id);
        return R.ok(resp);
    }

    //新增
    @ApiOperation(value = "新增课程")
    @PostMapping
    @PreAuthorize("@ss.hasPermi('app:course:add')")
    public R add(@RequestBody @Valid AppCourseReq req){
        boolean result=this.appCourseService.add(req);
        return super.isSuccess(result);
    }
    //编辑
    @ApiOperation(value = "编辑课程")
    @PutMapping
    @PreAuthorize("@ss.hasPermi('app:course:edit')")
    public R edit(@RequestBody @Valid AppCourseEditReq req){
        boolean result=this.appCourseService.edit(req);
        return super.isSuccess(result);
    }

    //删除操作
    @ApiOperation(value = "删除课程")
    @PreAuthorize("@ss.hasPermi('app:course:remove')")
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable(value = "ids") Long[] ids){
        boolean result=this.appCourseService.deleteById(ids);
        return super.isSuccess(result);
    }



    @PreAuthorize("@ss.hasPermi('admin:course:list')")
    @ApiOperation(value = "课程树形列表")
    @GetMapping(value = "/tree")
    public R courseTree(){
        List<AppCourseTreeResp> list=this.appCourseService.buildSubjectTree();
        return R.ok(list);
    }




}
